home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17410 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.9 KB

  1. Path: mail2news.demon.co.uk!j-bg.demon.co.uk
  2. From: John Sargent <jb@j-bg.demon.co.uk>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Array pointer?
  5. Date: Sun, 14 Apr 96 20:22:46 GMT
  6. Distribution: all
  7. Message-ID: <829513366snz@j-bg.demon.co.uk>
  8. References: <DppIHL.MzF@on.bell.ca>
  9. Reply-To: jb@j-bg.demon.co.uk
  10. X-NNTP-Posting-Host: j-bg.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.29
  12. X-Mail2News-Path: j-bg.demon.co.uk
  13.  
  14. In article <DppIHL.MzF@on.bell.ca> g2david@sprynet.com "David Leung" writes:
  15.  
  16. > if I write this
  17. >         int a[10];
  18. >         int *b1=a;  <--- The pointer b points to the head of the array?
  19.  
  20.           b1 points to the first element of the array. that is, b1 will
  21.           be the address of a[0]
  22.  
  23.  
  24.  
  25. >         int b2=a    <--- What will we get?
  26.  
  27.           a compiler error.
  28.           trying to assign an address ( that of a[0] ) to an int.
  29.  
  30.  
  31. >         int b3=*a   <--- what will we get?
  32.  
  33.           b3 will contain the contents of the first element of the array.  
  34.  
  35.  
  36. >         int *b4=*a  <--- what will we get again?                
  37.  
  38.           another compiler error.
  39.           trying to assign an int to a variable meant to contain an address.
  40.  
  41.  
  42.  
  43. >         ; access like a[3]
  44. >         int *c1;
  45. >         c1= a+3
  46. >         cout << *c1; ?
  47.             
  48.           OK. you'll get the contents of c1 ( a[3] )
  49.  
  50.  
  51. >         cout << c1;  ?
  52.  
  53.           You'll get c1 ( the address of a[3] )  
  54.  
  55.  
  56.  
  57. >         ; is this correct?
  58. >         int c2;
  59. >         
  60. >         c2 = *(a+3); ?
  61. >         cout << c2;  ?
  62.  
  63.           OK. you'll get the contents of a[3]  
  64.  
  65.  
  66. >         ; is this correct, too?
  67. >         int *c3
  68. >         
  69. >         c3 = (a + 3 * sizeof(int) );
  70. >         count << *c3;
  71.  
  72.           OK. you'll get the contents of a[3]  
  73.           a + 3 is the same as a + (3 * sizeof(int)) if a is an int *   
  74.  
  75. > I am very confused!
  76. > Can anyone explain those to me, please!
  77. > g2david
  78. > :->
  79.  
  80.  
  81. Regards,
  82. John Sargent
  83.